<template>
	<div>
		<van-nav-bar
			title="签到"
			left-arrow
			left-text="返回"
			fixed
			@click-left="$router.go(-1)"
		/>
        <!-- 这边学生和教师所展现的功能不一样，后续需要进行修改！！！！！！！ -->
        <van-row>
            <van-col span="24">
               <van-icon name="sign" size="3rem" color="#55aaff" />
            </van-col>
        </van-row>
        <van-row>
            <van-col span="24">
               <van-button round type="info" @click="goSignInDetail">{{signInText}}</van-button>
            </van-col>
        </van-row>
        <van-divider
            :style="{ color: '#f3f3f3', borderColor: '#555500', padding: '0 10px' }"
        >
		</van-divider>
        <van-cell-group title="历史签到">
            <van-divider
                :style="{ color: '#f3f3f3', borderColor: '#555500', padding: '0 10px' }"
            >
            </van-divider>
            <div v-if="this.userInfo.roles[0] === 'STUDENT'">
                <!-- <van-cell title="2021年1月10日15:50限时签到" value="已签到" />
                <van-cell title="2021年1月10日15:00手势签到" value="已签到" />
                <van-cell title="2021年1月10日14:00一键签到" value="已签到" label="已签到"/> -->
                <van-cell v-for="(r,index) in signInRecords" :title="r.time"   :value="r.flag?'已签到':'未签到'" 
                    :label="r.limited?'限时签到':'一键签到'"        
                    :key="index"  />
            </div>
            <div v-else>
                
                <!-- <van-cell v-for="(c,index) in courseInfo.records" :title="c.name" :key="index" 
									value="" :label="c.title" icon="photo-fail" center is-link @click="goDetailBanke(c)"/> -->
                <van-cell v-for="(r,index) in signInRecords" :title="r.createTime + ' | ' + (r.limited?'限时签到':'一键签到')"  :label="'总人数：' + r.allCount + ' || '  + '已签到人数：' + r.clockInCount"          
                    :key="index"  is-link @click="goSignInInfo(r)" />
            </div>
        </van-cell-group>
        
	</div>
</template>

<script>
	import { viewTaskForTeacher , viewTaskForStudent, viewSelfSignInInfo} from '@/api/signIn.js';
import { Toast } from 'vant';
	export default {
		components: {
			
		},
        created() {
            this.userInfo = JSON.parse(localStorage.getItem('userInfo'));
            this.detailBankeInfo = JSON.parse(localStorage.getItem('detailBankeInfo'));
            if(this.userInfo.roles[0] === 'TEACHER') {
                this.viewTaskForTeacherALL(this.detailBankeInfo.id);   
            } else {
                this.viewTaskForStudentALL(this.detailBankeInfo.id); 
                this.viewSignInHistory(this.detailBankeInfo.id); 
            }
            
            // console.log(this.signInRecords);
        },
		data() {
			return {
				userInfo: {},
                detailBankeInfo: {},
				signInRecords: [],
                taskTotal: 20,
                studentHaveTask: false,
                teacherHaveTask: false,
			}
		},
		computed: {
			signInText() {
                return this.userInfo.roles[0] === 'TEACHER'?'发起签到':'参与签到';
            }
		},
		methods: {
            goSignInDetail() {
                if(this.userInfo.roles[0] === 'TEACHER') { // 老师 去发起签到页
                    this.$router.push({
                        name: 'signInDetail',					
                    });
                } else { // 学生 去参与签到页
                    if(this.studentHaveTask) { 
                        this.$router.push({
                            name: 'signInDetail',					
                        });
                    }                    
                    else {
                        Toast('暂无签到任务！');
                    }
                }
                
            },
            viewSignInHistory(courseId) { // 学生查看历史签到信息
                viewSelfSignInInfo(courseId).then(res => {
                    console.log(res);
                    if(res.data.code === 0) {
                        this.signInRecords = res.data.data;
                    }
                })
            },

            async viewTaskForTeacherALL(courseId) {  //  分页查看所有任务
                let curPage = 1;                
                while(this.taskTotal > (curPage-1)*10) {
                    await viewTaskForTeacher(curPage ,10 ,courseId).then(res => {
                        console.log(res);
                        if(res.data.code === 0) {
                            this.taskTotal = res.data.data.total;
                            for(let r of res.data.data.records) {
                                if(!r.ended) {
                                    this.teacherHaveTask = true;
                                    localStorage.setItem('currentTask' , JSON.stringify(r));   
                                }
                                this.signInRecords.push(r);
                            }
                                               
                        } else {
                            this.taskTotal = 0;
                        }
                    }).catch(err => console.log(err));
                    curPage++;
                }
                if(!this.teacherHaveTask) localStorage.removeItem('currentTask') 
                this.signInRecords = this.signInRecords.reverse();                   
            },
            
            async viewTaskForStudentALL(courseId) { // 存下所有任务信息，并且找找看有没有签到任务 this.studentHaveTask = true;
                let curPage = 1;                
                while(this.taskTotal > (curPage-1)*10) {
                    await viewTaskForStudent(curPage ,10 ,courseId).then(res => {
                        // console.log(res);
                        if(res.data.code === 0) {
                            this.taskTotal = res.data.data.total;
                            for(let r of res.data.data.records) {
                                if(!r.ended) {
                                    this.studentHaveTask = true;
                                    console.log('有签到任务')
                                }
                                // this.signInRecords.push(r);
                            }
                                                   
                        } else {
                            this.taskTotal = 0;
                        }
                    }).catch(err => console.log(err));
                    curPage++;
                }
                this.signInRecords = this.signInRecords.reverse();                
            },
            goSignInInfo(r) {
                // localStorage.setItem('BKdetail' , JSON.stringify(c));
				
				localStorage.setItem('signInInfo' , JSON.stringify(r));
				this.$router.push({
					name: 'signInInfo',					
				});
            }
			
		}
	}
</script>

<style>
</style>
